package com.ruoyi.jjyh.controller;

import java.util.List;

import com.ruoyi.jjyh.domain.JjyhMassage;
import com.ruoyi.jjyh.domain.SysDtArea;
import com.ruoyi.jjyh.service.IJjyhMassageService;
import com.ruoyi.jjyh.service.ISysDtAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjyh.domain.JjyhGoodsrelease;
import com.ruoyi.jjyh.service.IJjyhGoodsreleaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品发布Controller
 * 
 * @author ruoyi
 * @date 2020-11-20
 */
@Controller
@RequestMapping("/jjyh/goodsrelease")
public class JjyhGoodsreleaseController extends BaseController
{
    private String prefix = "jjyh/goodsrelease";

    @Autowired
    private IJjyhGoodsreleaseService jjyhGoodsreleaseService;
    @Autowired
    private ISysDtAreaService sysDtAreaService;
    @Autowired
    private IJjyhMassageService jjyhMassageService;

    @RequiresPermissions("jjyh:goodsrelease:view")
    @GetMapping()
    public String goodsrelease()
    {
        return prefix + "/goodsrelease";
    }

    /**
     * 查询商品发布列表
     */
    @RequiresPermissions("jjyh:goodsrelease:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JjyhGoodsrelease jjyhGoodsrelease)
    {
        startPage();
        List<JjyhGoodsrelease> list = jjyhGoodsreleaseService.selectJjyhGoodsreleaseList(jjyhGoodsrelease);
        return getDataTable(list);
    }

    /**
     * 导出商品发布列表
     */
    @RequiresPermissions("jjyh:goodsrelease:export")
    @Log(title = "商品发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JjyhGoodsrelease jjyhGoodsrelease)
    {
        List<JjyhGoodsrelease> list = jjyhGoodsreleaseService.selectJjyhGoodsreleaseList(jjyhGoodsrelease);
        ExcelUtil<JjyhGoodsrelease> util = new ExcelUtil<JjyhGoodsrelease>(JjyhGoodsrelease.class);
        return util.exportExcel(list, "goodsrelease");
    }

    /**
     * 新增商品发布
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
 /**
     * 新增商品发布
     */
    @GetMapping("/goodsSelect")
    public String goodsSelect()
    {
        return prefix + "/goodsSelect";
    }

    /**
     * 新增保存商品发布
     */
    @RequiresPermissions("jjyh:goodsrelease:add")
    @Log(title = "商品发布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JjyhGoodsrelease jjyhGoodsrelease)
    {
        return toAjax(jjyhGoodsreleaseService.insertJjyhGoodsrelease(jjyhGoodsrelease));
    }

    /**
     * 修改商品发布
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhGoodsrelease jjyhGoodsrelease = jjyhGoodsreleaseService.selectJjyhGoodsreleaseById(id);
        mmap.put("jjyhGoodsrelease", jjyhGoodsrelease);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品发布
     */
    @RequiresPermissions("jjyh:goodsrelease:edit")
    @Log(title = "商品发布", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JjyhGoodsrelease jjyhGoodsrelease)
    {
        return toAjax(jjyhGoodsreleaseService.updateJjyhGoodsrelease(jjyhGoodsrelease));
    }

    /**
     * 删除商品发布
     */
    @RequiresPermissions("jjyh:goodsrelease:remove")
    @Log(title = "商品发布", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jjyhGoodsreleaseService.deleteJjyhGoodsreleaseByIds(ids));
    }

    /**
     * 获取省
     */
    @GetMapping("/{parentId}")
    @ResponseBody
    public TableDataInfo provinceData(@PathVariable("parentId") String parentId)
    {
        SysDtArea sysDtArea = new SysDtArea();
        sysDtArea.setAreaParentId(parentId);
        List<SysDtArea> proList = sysDtAreaService.selectSysDtAreaList(sysDtArea);
        return getDataTable(proList);
    }

    /**
     * 查看商品
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhGoodsrelease jjyhGoodsrelease = jjyhGoodsreleaseService.selectJjyhGoodsreleaseById(id);
        mmap.put("jjyhGoodsrelease", jjyhGoodsrelease);
        return prefix + "/view";
    }
    /**
     * 商品审核
     */
    @GetMapping("/reviewGoods")
    public String reviewGoods()
    {
        return prefix + "/reviewGoods";
    }
    /**
     * 查询商品审核列表
     */
    @RequiresPermissions("jjyh:goodsrelease:list")
    @PostMapping("/listReview")
    @ResponseBody
    public TableDataInfo listReview(JjyhGoodsrelease jjyhGoodsrelease)
    {
        startPage();
        jjyhGoodsrelease.setGoodsStatus("审核中");
        List<JjyhGoodsrelease> list = jjyhGoodsreleaseService.selectJjyhGoodsreleaseList(jjyhGoodsrelease);
        return getDataTable(list);
    }

    /**
     * 拒绝
     */
    @GetMapping("/review/{id}")
    public String editReview(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhGoodsrelease jjyhGoodsrelease = jjyhGoodsreleaseService.selectJjyhGoodsreleaseById(id);
        mmap.put("jjyhGoodsrelease", jjyhGoodsrelease);
        return prefix + "/review";
    }

    /**
     * 拒绝商品
     */
    @RequiresPermissions("jjyh:goodsrelease:list")
    @PostMapping("/reviewGoodsDis")
    @ResponseBody
    public AjaxResult reviewGoodsDis(JjyhGoodsrelease jjyhGoodsrelease)
    {
        jjyhGoodsrelease.setGoodsStatus("审核未通过");
        int num = jjyhGoodsreleaseService.updateJjyhGoodsrelease(jjyhGoodsrelease);
        JjyhGoodsrelease jjyhGoodsrelease1 = jjyhGoodsreleaseService.selectJjyhGoodsreleaseById(jjyhGoodsrelease.getId());
        if(num >0){
            //添加个人消息
            JjyhMassage jjyhMassage = new JjyhMassage();
            jjyhMassage.setType("2");
            jjyhMassage.setUserId(jjyhGoodsrelease1.getUserid());
            jjyhMassage.setContent(jjyhGoodsrelease.getDisReason());
            jjyhMassage.setTitle("审核未通过");
            jjyhMassageService.insertJjyhMassage(jjyhMassage);
        }
        return toAjax(num);
    }

    /**
     * 通过商品
     */
    @RequiresPermissions("jjyh:goodsrelease:list")
    @PostMapping("/reviewOk")
    @ResponseBody
    public AjaxResult reviewOk(String id)
    {
        JjyhGoodsrelease jjyhGoodsrelease = new JjyhGoodsrelease();
        jjyhGoodsrelease.setId(id);
        jjyhGoodsrelease.setGoodsStatus("上架");
        int num = jjyhGoodsreleaseService.updateJjyhGoodsrelease(jjyhGoodsrelease);
        JjyhGoodsrelease jjyhGoodsrelease1 = jjyhGoodsreleaseService.selectJjyhGoodsreleaseById(jjyhGoodsrelease.getId());
        if(num >0){
            //添加个人消息
            //添加个人消息
            JjyhMassage jjyhMassage = new JjyhMassage();
            jjyhMassage.setType("2");
            jjyhMassage.setUserId(jjyhGoodsrelease1.getUserid());
            jjyhMassage.setContent("商品‘"+jjyhGoodsrelease1.getGoodsTitle()+"'已通过审核");
            jjyhMassage.setTitle("审核通过");
            jjyhMassageService.insertJjyhMassage(jjyhMassage);
        }
        return toAjax(num);
    }
}
